﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierAnimator : BattleComponent<SoldierAnimator> {
    public FSG.MeshAnimator.MeshAnimator meshAnimator;
    public SoldierAI soldierAI;
    public Warrior warrior;
    public string CurrentanimationName;
    public float animationProgress;
    

    private void Start()
    {
        meshAnimator.LODCamera = BattleManager.Instance.LODCamera.transform;
    }

    private void OnEnable()
    {
        Assign(this);
    }
    private void OnDisable()
    {
        Unassign(this);
    }


    public static void UpdateAnimator()
    {
        foreach(var sa in ComponentDataList)
        {
            sa.UpdateAnimation();
        }
    }

    public void UpdateAnimation()
    {
        switch (soldierAI.currentState)
        {
            case SoldierState.Moving:
                CurrentanimationName = "Run";
                break;
            case SoldierState.MeleeAttacking:
                CurrentanimationName = "Attack01";
                break;
            case SoldierState.RangedAttacking:
                if(soldierAI.timer.IsTimerFinish(warrior.rangedAttackShootTime))
                {
                    CurrentanimationName = "Attack02";
                }
                else
                {
                    CurrentanimationName = "Idle";
                }
                break;
            case SoldierState.Locking:
                if(soldierAI.lockController.lockingtype== SoldierLockingtype.ToDie || soldierAI.lockController.lockingtype == SoldierLockingtype.HitFly)
                {
                    CurrentanimationName = "Death";
                }
                if(soldierAI.lockController.lockingtype== SoldierLockingtype.Defend)
                {
                    CurrentanimationName = "GetHit";
                }
                break;
            case SoldierState.Idle:
                CurrentanimationName = "Idle";
                break;
            case SoldierState.Preparing:
                CurrentanimationName = "Walk";
                break;
            case SoldierState.Dead:
                CurrentanimationName = "Death";
                break;
        }
        meshAnimator.Play(CurrentanimationName);

        if (soldierAI.currentState == SoldierState.MeleeAttacking)
        {
            meshAnimator.currentAnimation.length = warrior.meleeAttackTime;
        }

        if(soldierAI.currentState == SoldierState.RangedAttacking && soldierAI.timer.IsTimerFinish(warrior.rangedAttackShootTime))
        {
            meshAnimator.currentAnimation.length = warrior.rangedAttackTime-warrior.rangedAttackShootTime;
        }
    }
}
